package com.hee.erp.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hee.erp.filter.LoginContextHolder;
import com.hee.erp.pojo.LoginInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LoginInfo loginInfo = LoginContextHolder.get();
        this.strictInsertFill(metaObject, "delFlag", () -> false,Boolean.class);
        this.strictInsertFill(metaObject, "createUser",() -> loginInfo.getUserId(),String.class);
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "modifyUser",() -> loginInfo.getUserId(),String.class);
        this.strictInsertFill(metaObject, "modifyTime", () -> LocalDateTime.now(), LocalDateTime.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LoginInfo loginInfo = LoginContextHolder.get();
        this.strictInsertFill(metaObject, "modifyUser",() -> loginInfo.getUserId(),String.class);
        this.strictInsertFill(metaObject, "modifyTime", () -> LocalDateTime.now(), LocalDateTime.class);
    }
}
